package com.desire.test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * @author desire
 */
public class _29_裁剪图像 {
    public static void main(String[] args) throws Exception {
        String p = "C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\刻晴.png";
        BufferedImage read = ImageIO.read(new File(p));
        int sw = read.getWidth();
        int sh = read.getHeight();


        // 创建裁切后的图片大小
        int dw = 500;
        int dh = (int) (sh * (1.0 * dw / sw));
        BufferedImage image = new BufferedImage(dw, dh, 2);
        // 根据裁切后的图像创建画笔
        Graphics2D g = image.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

        // 去截取图片然后写入到创建的当中
        int x = 1000;
        int y = 300;
        g.drawImage(read, 0, 0, dw, dh, x, y, x + dw, y + dh, null);
        g.dispose();

        String to = "C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\刻晴1.png";

        ImageIO.write(image, p.substring(p.lastIndexOf(".") + 1), new File(to));
        Runtime run = Runtime.getRuntime();
        run.exec("cmd /k start " + to);
    }
}
